package com.mephi.oshs;

import com.mephi.oshs.model.tree.OshsTree;
import com.mephi.oshs.presenter.PresenterManager;
import com.mephi.oshs.view.api.IViewManager;
import com.mephi.oshs.view.api.ViewId;
import com.mephi.oshs.view.impl.OshsView;
import com.mephi.oshs.view.impl.ViewManager;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.IFolderLayout;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.mephi.oshs.view.api.ViewId.*;

/**
 * Данный класс отвечает за создание окна плагина и всех его представлений.
 * This class provides a functionality for creation plugin window and it's views.
 */
public class Main {
    private static final Logger logger = LoggerFactory.getLogger(Main.class);
    public static final IViewManager VIEW_MANAGER = ViewManager.getInstance();

    /**
     * Список вкладок - порядок в списке определяет порядок следования вкладок.
     */
    private static final ViewId[] TOOLS_VIEW_IDS = {
            EXPLORE_VIEW,
            METAINFO_VIEW,
            SEARCH_VIEW,
            FILTER_VIEW,
            MANAGEMENT_VIEW
    };

    private static BundleContext bundleContext;

    /**
     * This method is calling when plugin window was created.
     * It tunes new window: resize, disable status bar and etc.
     *
     * @param configurer IWorkbenchWindowConfigurer
     */
    public static void onWindowsCreation(final IWorkbenchWindowConfigurer configurer) {
        configurer.setInitialSize(new Point(640, 480));
        configurer.setShowCoolBar(false);
        configurer.setShowStatusLine(false);

        // Инициализация дерева ОШС.
        logger.debug("onWindowsCreation(): Oshs tree initialization...");
        OshsTree.getInstance();
    }

    /**
     * This method is responsible for creation all plugin views.
     *
     * @param layout IPageLayout - main window.
     * @see Main#TOOLS_VIEW_IDS
     */
    public static void onViewsCreation(final IPageLayout layout) {
        final String ref = layout.getEditorArea();

        // Create Oshs tree view.
        layout.addView(OshsView.ID.name(), IPageLayout.LEFT, 0.35f, ref);
        layout.getViewLayout(OshsView.ID.name()).setCloseable(false);

        /* Create Tools View.

           Create a folder for multiple analysis views so that they will open up
           in tabs next to the table view.*/
        final IFolderLayout folder = layout.createFolder("Tables", IPageLayout.LEFT, 0, ref);
        for (final ViewId viewId : TOOLS_VIEW_IDS) {
            addCloseableView(layout, folder, viewId.name());
        }
    }

    /**
     * This method is calling when plugin window and all it's views are fully completed.
     * This method starts plugin logic.
     */
    public static void onPluginReady() {
        PresenterManager.getInstance(VIEW_MANAGER).show();
    }

    /**
     * Method creates tabbed view by it's id, without close button on it.
     *
     * @param layout Main window layout.
     * @param folder Layout of tabbed element.
     * @param viewID View ID.
     */
    private static void addCloseableView(final IPageLayout layout, final IFolderLayout folder, final String viewID) {
        folder.addView(viewID);
        layout.getViewLayout(viewID).setCloseable(false);
    }

    public static BundleContext getBundleContext() {
        return bundleContext;
    }

    public static void setBundleContext(BundleContext bundleContext) {
        Main.bundleContext = bundleContext;
    }
}
